<?php
// +----------------------------------------------------------------------+
// | Author:  Evgeny Leontev <eleontev@gmail.com>                         |
// | Copyright (c) 2005 Evgeny Leontev                                    |
// +----------------------------------------------------------------------+
// | This source file is free software; you can redistribute it and/or    |
// | modify it under the terms of the GNU Lesser General Public           |
// | License as published by the Free Software Foundation; either         |
// | version 2.1 of the License, or (at your option) any later version.   |
// |                                                                      |
// | This source file is distributed in the hope that it will be useful,  |
// | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    |
// | Lesser General Public License for more details.                      |
// +----------------------------------------------------------------------+


class MyDir
{
	
	var $one_level = true;  // true = all, dir, file, false = recursively
	var $full_path = false;
	
	var $skip_dirs = array();
	var $skip_files = array();
	
	var $allowed_extension = array();
	var $denied_extension 	= array();
	
	

   /**
	* Read $dirname, return multi array with files and dirs on success
	*
	* @param       string   $dirname    The directory to read
	* @return      bool     Returns true on success, false on failure
	*/
	function &getFilesDirs($dirname = '.', $output = array()){
		
		if (@$handle = opendir($dirname)) {
			while(false !== ($entry = readdir($handle))) {
			
				if($entry == '.' || $entry == '..') {
					continue; 
				}
				
				if(in_array($entry, $this->skip_dirs)) {
					continue; 
				}
				
				if(in_array($entry, $this->skip_files)) {
					continue; 
				}
				
				$path = $dirname.'/'.$entry;
				$path = str_replace('//', '/', $path);
				//echo "<pre>"; print_r($path); echo "</pre>";
				//echo "<pre>"; print_r($entry); echo "</pre>";
				
				if(is_dir($path)) {
					
					if($this->one_level) {
						continue;
					}
					
					if(in_array($path, $this->skip_dirs)) { 
						continue; 
					}					
				
					if($this->full_path) { $output = $this->getFilesDirs($path, $output) + $output;  }
					else                 { $output[$entry] = $this->getFilesDirs($path);    }
					
				} elseif(is_file($path)) {
					
					$extension = substr($path, strrpos($path, '.')+1);
					
					if(in_array($path, $this->skip_files)) { 
						continue;
					
					//check for allowed extension
					} elseif($this->allowed_extension && !in_array($extension, $this->allowed_extension)){
						//$uploaded['bad']['wrong_extension'][$v['name']] = $v['extension'];
						continue;
					
					//check for not denied extension
					} elseif($this->denied_extension && in_array($extension, $this->denied_extension)){
						//$uploaded['bad']['wrong_extension'][$v['name']] = $v['extension'];
						continue;
					}
					
					if($this->full_path) { $output[] = $path; }
					else                 { $output[] = $entry; }
				}
			}
			
			return isset($output) ? $output : array();
			closedir($handle);
		
		} else {
			return false;
		}
	}	
	
	
	
   /**
	* Delete a file, or a folder and its contents
	*
	* @param       string   $dirname    The directory to delete
	* @return      bool     Returns true on success, false on failure
	*/
	function removeFilesDirs($dirname = '.') {
		
		if (is_file($dirname)) { return unlink($dirname); }
		
		$dir = dir($dirname);
		while (false !== $entry = $dir->read()) {
			if ($entry == '.' || $entry == '..' || in_array($entry, $this->skip_dirs)) { continue; }
			
			// Deep delete directories  
			$path = $dirname . '/' . $entry;
			if (is_dir($path)) { $this->removeFilesDirs($path); }
			else               { unlink($path); }
		}
		
		$dir->close();
		return rmdir($dirname);
	}
	

   /**
	* Sets directories to skip (not parse)
	*
	* @param       mixed   $extension    directory name
	* @return      void     
	*/
	function setSkipDirs($dirname) {
		$dirname = (is_array($dirname)) ? $dirname : func_get_args();
		foreach($dirname as $k => $v) {
			$this->skip_dirs[] = $v;
		}
	}
	

   /**
	* Sets files to skip (not parse)
	* for filename use top_dir/sub_dir/filename.php 
	*
	* @param       mixed   $extension    filename
	* @return      void     
	*/
	function setSkipFiles($filename) {
		$filename = (is_array($filename)) ? $filename : func_get_args();
		foreach($filename as $k => $v) {
			$this->skip_files[] = $v;
		}
	}

	
   /**
	* Define allowed extensions
	*
	* @param       mixed   $extension    allowed extesions
	* @return      void     
	*/	
	function setAllowedExtension($extension) {
		$extension = (is_array($extension)) ? $extension : func_get_args();
		foreach($extension as $k => $v) {
			$this->allowed_extension[] = str_replace('.', '', $v);
		}
	}
	
	
	function setDeniedExtension($extension) {
		$extension = (is_array($extension)) ? $extension : func_get_args();
		foreach($extension as $k => $v) {
			$this->denied_extension[] = str_replace('.', '', $v);
		}
	}		
	
	
	function getFileExtension($path) {
		return substr($path, strrpos($path, '.')+1);
	}
	
	function getFilename($path) {
		return basename($path);
	}
	
	function getFileDirectory($path) {
		return dirname($path);
	}
	
	function _toArray($val) {
		if(!is_string($val[0])) { return $val[0]; }
		return $val;
	}
}


/*
$d = &new MyDir;
//$d->setSkipDirs('.svn', 'cvs','.SVN', 'CVS');
//$d->setAllowedExtension('php', 'ini');

//$a = &$d->getFilesDirs('e:/www/localhost/a_repository/eleontev');
$a = &$d->getFilesDirs('e:/www/localhost/project/kb');
//echo "<pre>"; print_r($d); echo "</pre>";
echo "<pre>"; print_r($a); echo "</pre>";
*/


/*
function multiArrayToString($arr, $glue=',') { 
	foreach($arr as $k => $v) {
		if(is_array($v)) { 
			$arr[$k] = multiArrayToString($v, $glue); 
		}
	}
	return (join($glue, $arr));
} 
*/

/*
function multiArrayToString($arr, $glue='/', $dir = 'sdd/jahsdshdk', $output = array()) {
	foreach($arr as $_dir => $v) {
		if(is_array($v)) {
			$d[$_dir] = $_dir;
			$output = multiArrayToString($v, $glue, $d, $output) + $output;
		} else {
			$dir = (is_array($dir)) ? implode('/', $dir) . '/' : '';
			$output[] = $dir . $v;
		} 
	}
	
	return $output;
}

echo "<pre>"; print_r(multiArrayToString($a)); echo "</pre>";
*/

?>